import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/',
      name: 'Layout',
      redirect: "/home",
      // 路由懒加载
      component: () => import(/* webpackChunkName: "Layout" */ '@/page/Layout'),
      children: [
        {
          path: 'home',
          name: 'Home',
          meta: {
            requiresAuth: false,
            title: '首页'
          },
          // 路由懒加载
          component: () => import(/* webpackChunkName: "home" */ '@/page/Home')
        },
        {
          path: 'me',
          name: 'Me',
          meta: {
            requiresAuth: true,
            title: '我的'
          },
          component: () => import(/* webpackChunkName: "Me" */ '@/page/Me')
        },
        {
          path: 'square',
          name: 'Square',
          meta: {
            requiresAuth: true,
            title: '广场'
          },
          // 路由懒加载
          component: () => import(/* webpackChunkName: "square" */ '@/page/Square')
        },
        {
          path: 'info',
          name: 'info',
          meta: {
            requiresAuth: true,
            title: '详情'
          },
          // 路由懒加载
          component: () => import(/* webpackChunkName: "info" */ '@/page/info')
        },
      ]
    },
    {
      path: '/push',
      name: 'Push',
      meta: {
        requiresAuth: true,
        title: '发布'
      },
      // 路由懒加载
      component: () => import(/* webpackChunkName: "push" */ '@/page/Push')
    },
    {
      path: '/history',
      name: 'history',
      meta: {
        requiresAuth: true,
        title: '历史记录'
      },
      // 路由懒加载
      component: () => import(/* webpackChunkName: "history" */ '@/page/history')
    },
    {
      path: '/user',
      name: 'User',
      meta: {
        requiresAuth: false,
        title: '登录/注册'
      },
      // 路由懒加载
      component: () => import(/* webpackChunkName: "user" */ '@/page/User')
    },
    {
      path: '/Individual',
      name: 'ind',
      meta: {
        requiresAuth: true,
        title: '个人编辑页面'
      },
      // 路由懒加载
      component: () => import(/* webpackChunkName: "user" */ '@/page/Individual')
    }
    , {
      path: '/Inquire',
      name: 'inq',
      meta: {
        requiresAuth: false,
        title: '个人编辑页面'
      },
      // 路由懒加载
      component: () => import(/* webpackChunkName: "user" */ '@/page/Inquire')
    }
  ]
})


// 登录状态检查
router.beforeEach((to, from, next) => {
  document.title = to.meta.title;
  // 未登录
  if (localStorage.getItem("token") == null) {
    // 你访问的页面还需要登录
    
    if (to.meta.requiresAuth) {
      next({
        name: 'User',
        query: {
          from: to.fullPath
        }
      })
     
    } else {
      next()
    }
  } else {
    next()
  }
})

//处理动态路由


export default router